<?
$font = $_GET['font'] ? $_GET['font'] : "Gentium";
$text = $_GET['text'] ? $_GET['text'] : "HelloWorld";
$size = $_GET['size'] ? (int) $_GET['size'] : 14;
$padding = $_GET['padding'] ? (int) $_GET['padding'] : 5;
$color = $_GET['color'] ? $_GET['color'] : "000000";

$img_hash = md5($font.$text.$size.$padding.$color);
$cache_file = "header_cache/$img_hash.png";


if(preg_match("/^\\\\\\\\x..$/", trim($text)))
	$text = chr(hexdec(substr($text,-2)));

if(file_exists($cache_file) && false){
	header('Content-type: image/png');
	readfile($cache_file);
}
else{

	putenv('GDFONTPATH=' . realpath('.'));

	$r = hexdec(substr($color,0,2));
	$g = hexdec(substr($color,2,2));
	$b = hexdec(substr($color,4));

	$s = imagettfbbox($size, 0, $font, $text);
	$w = 2*$padding + abs($s[0]) + abs($s[2]);
	$h = 2*$padding + abs($s[1]) + abs($s[5]);
	$x = $padding;
	$y = $padding + abs($s[5]);

	$im  = imagecreatetruecolor($w,$h);
	imagesavealpha($im, true);
	imagealphablending($im, false);

	$color = imagecolorallocate($im, $r, $g, $b);
	$back = imagecolorallocatealpha($im, 255-$r, 255-$g, 255-$b, 127);

	imagefill($im, 0, 0, $back);
	imagettftext($im, $size, 0, $x, $y, $color, $font, $text);

	header('Content-type: image/png');
	imagepng($im);
	imagepng($im, $cache_file, 9);

	imagedestroy($im);
}
?>
